//
//  LeftViewController.m
//  IntimateGift
//
//  Created by zh on 14/12/17.
//  Copyright (c) 2014年 蔡宏业. All rights reserved.
//

#import "LeftViewController.h"
#import "CategoryViewController.h"
#import "ChoicenessViewController.h"
#import "MineViewController.h"
#import "UIViewController+MMDrawerController.h"
#import "UIColor+AddColor.h"

#define LEFTREUSE @"LeftReuse"

@interface LeftViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *myTableView;
// 存储tableView的cell标签
@property (nonatomic, retain) NSMutableArray *arr;

@property (nonatomic, assign) BOOL state;

@property (nonatomic, retain) NSDictionary *dic;


@end

@implementation LeftViewController


- (id)init
{
    self = [super init];
    if (self) {
        [self setRestorationIdentifier:@"MMExampleLeftSideDrawerController"];
        self.arr = [NSMutableArray arrayWithObjects:@"精选", @"分类", @"我的", nil];
        self.state = NO;
        self.dic = [NSDictionary dictionary];
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        [center addObserver:self selector:@selector(receiveNotifica:) name:@"tamenlaile" object:nil];
        NSNotificationCenter *center2 = [NSNotificationCenter defaultCenter];
        [center2 addObserver:self selector:@selector(receiveNotifica2:) name:@"tamenlaile2" object:nil];
    }
    return self;
}
- (void)receiveNotifica:(NSNotification *)center
{
    self.state = YES;
    self.dic = [center.userInfo objectForKey:@"key"];
}

- (void)receiveNotifica2:(NSNotification *)center
{
    self.state = NO;
//    self.dic = [center.userInfo objectForKey:@"key"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"菜单";
    
    // tableView背景
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    UIImage *image = [UIImage imageNamed:@"left2.jpg"];
    imageView.image = image;
    [self.view addSubview:imageView];
    
    [imageView release];
    
    // tableView设置
    CGRect rect = self.view.bounds;
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height - rect.size.height * 0.4) style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.backgroundColor = [UIColor clearColor];
    self.myTableView.scrollEnabled = NO;
    
    // 标题
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
    view.backgroundColor = [UIColor clearColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, view.bounds.size.width, 64 - 10)];
    [view addSubview:titleLabel];
    titleLabel.text = @"温馨礼物";
    titleLabel.textAlignment = 0;
    titleLabel.textColor = [UIColor titleColor];
    titleLabel.font = [UIFont systemFontOfSize:25];
    
    [titleLabel release];
    
    [self.myTableView.tableHeaderView addSubview:view];
    [self.view addSubview:self.myTableView];
    self.myTableView.tableHeaderView = view;
    // 注册cell
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:LEFTREUSE];
//    ChoicenessViewController *choicenessVC = [[ChoicenessViewController alloc] init];
//    [self.mm_drawerController setCenterViewController:choicenessVC withCloseAnimation:YES completion:^(BOOL finished) {
//        
//        
//        
//    }];
    [view release];
    [_myTableView release];
    
//    // 图标
//    UIImageView *appImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - self.view.bounds.size.width * 0.87, self.view.bounds.size.height - self.view.bounds.size.height * 0.18, self.view.bounds.size.width * 0.18, self.view.bounds.size.width * 0.18)];
//    [self.view addSubview:appImageView];
//    
////    appImageView.backgroundColor = [UIColor clearColor];
//    appImageView.image = [UIImage imageNamed:@"iconfont-giftvector.png"];
////    appImageView.layer.cornerRadius = 11;
////    appImageView.clipsToBounds = YES;
//    [appImageView release];
//    // 版本号
//    UILabel *vLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - self.view.bounds.size.width * 0.89, self.view.bounds.size.height - self.view.bounds.size.height * 0.075, self.view.bounds.size.width * 0.22, self.view.bounds.size.width * 0.1)];
//    [self.view addSubview:vLabel];
//    vLabel.textAlignment = 1;
//    vLabel.text = @"v1.0";
//    vLabel.textColor = [UIColor titleColor];
//    vLabel.backgroundColor = [UIColor clearColor];
//    vLabel.font = [UIFont systemFontOfSize:20];
//    [vLabel release];
    
    // 夜间模式
    UIImageView *nightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, self.view.bounds.size.height - self.view.bounds.size.height * 0.385, 32, 32)];
//    nightImageView.backgroundColor = [UIColor redColor];
    nightImageView.image = [UIImage imageNamed:@"iconfont-yejianzhuanhuan.png"];
    [self.view addSubview:nightImageView];
    [nightImageView release];
    
    UILabel *nightLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, self.view.bounds.size.height - self.view.bounds.size.height * 0.4, 100, 50)];
    [self.view addSubview:nightLabel];
    nightLabel.text = @"夜间模式";
    nightLabel.textColor = [UIColor whiteColor];
    nightLabel.textAlignment = 1;
//    nightLabel.backgroundColor = [UIColor yellowColor];
    [nightLabel release];
    
    // 夜间模式开关
    UISwitch *openNight = [[UISwitch alloc] initWithFrame:CGRectMake(65, self.view.bounds.size.height - self.view.bounds.size.height * 0.33, 60, 60)];
    [openNight setOn:NO];
    openNight.on = NO;
    openNight.onTintColor = [UIColor ownColor];
    [self.view addSubview:openNight];
    [openNight addTarget:self action:@selector(openNightClicked:) forControlEvents:UIControlEventValueChanged];
    
    
    
    
    
//    self.view.backgroundColor     = [UIColor grayColor];
    
    // Do any additional setup after loading the view.
}
// 夜间模式开关方法
- (void)openNightClicked:(id)sender
{
    UISwitch *Switch = (UISwitch *)sender;
    BOOL button = [Switch isOn];
    if (button) {
//        NSLog(@"是");
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"heiyejianglin" object:@"香皂" userInfo: dic];
    }else {
//        NSLog(@"否");
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"limingjianglin" object:@"香皂" userInfo: dic];
    }
}

// cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
// 重用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 设置cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:LEFTREUSE];
    NSString *title = [self.arr objectAtIndex:indexPath.row];
    cell.textLabel.text = title;
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    if (indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-choice.png"];
    } else if (indexPath.row == 1) {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-fenlei.png"];
    } else if (indexPath.row == 2) {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-wode.png"];
    } else if (indexPath.row == 3) {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-wode.png"];
    }
    return cell;
}

// cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}
// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        ChoicenessViewController *choicenessVC = [[ChoicenessViewController alloc] init];
        
        UINavigationController *navigaChoiceness = [[UINavigationController alloc] initWithRootViewController:choicenessVC];
        
        [self.mm_drawerController setCenterViewController:navigaChoiceness withCloseAnimation:YES completion:^(BOOL finished) {
           
            
        }];
        
        
        [choicenessVC release];
        [navigaChoiceness release];
    } else if (indexPath.row == 1) {
        
        CategoryViewController *categoryVC = [[CategoryViewController alloc] init];
        
        UINavigationController *navigaCategory = [[UINavigationController alloc] initWithRootViewController:categoryVC];
        [self.mm_drawerController setCenterViewController:navigaCategory withCloseAnimation:YES completion:^(BOOL finished) {
            
            
            
        }];
        
        [categoryVC release];
        [navigaCategory release];
    } else if (indexPath.row == 2) {
        
        MineViewController *mineVC = [[MineViewController alloc] init];
        UINavigationController *navigaMine = [[UINavigationController alloc] initWithRootViewController:mineVC];
        [self.mm_drawerController setCenterViewController:navigaMine withCloseAnimation:YES completion:^(BOOL finished) {
            
            
            if(self.state == YES){
                
                //                NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
                [[NSNotificationCenter defaultCenter]postNotificationName:@"tamenlaile1" object:@"香皂" userInfo:self.dic];
//                NSLog(@"%@", self.dic);
            }
        }];

        [mineVC release];
        [navigaMine release];
        
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)dealloc
{
    [_myTableView release];
    [_arr release];
    [_dic release];
    [super dealloc];
}

@end
